﻿using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Entities.Modules;

using Findy.XsltDb;

public partial class DesktopModules_XsltDb_PackageXsltDb : PortalModuleBase
{
    static string root = HttpContext.Current.Server.MapPath("~/DesktopModules");
    static string renderers = HttpContext.Current.Server.MapPath("~/DesktopModules/XsltDb/Renderers");

    const string fnViewer = "XsltDb.ascx";
    const string fnSettings = "XsltDbSettings.ascx";
    const string fnLink = "XsltDbLink.xml";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var empty = new ListItem(" < Select a folder OR input new folder name > ", string.Empty);
            FolderSelect.Items.Add(empty);
            PickDirectories(root, "");
        }
    }

    void PickDirectories(string root, string current)
    {
        foreach (string dir in Directory.GetDirectories(Path.Combine(root, current)))
        {
            string cur = dir.Substring(root.Length + 1);
            string end = dir.Substring(dir.LastIndexOf("\\")+1);
            if (end.StartsWith("App_")) continue;
            if (end == "Providers") continue;
            if (end == "Admin" && current.Length == 0) continue;
            if (end == "XsltDb" && current.Length == 0) continue;
            if (end == "HTML" && current.Length == 0) continue;
            if (end == "AuthenticationServices" && current.Length == 0) continue;
            FolderSelect.Items.Add(cur);
            PickDirectories(root, cur);
        }
    }

    protected void lnkCancel_Command(object sender, CommandEventArgs e)
    {
        Return();
    }
    protected void lnkCopy_Command(object sender, CommandEventArgs e)
    {
        string folder = null;
        if (!string.IsNullOrEmpty(FolderSelect.SelectedValue))
            folder = FolderSelect.SelectedValue;
        if (!string.IsNullOrEmpty(FolderInput.Text))
            folder = FolderInput.Text;

        if (string.IsNullOrEmpty(folder))
            return;

        folder = Path.Combine(root, folder);

        if (!Directory.Exists(folder))
            Directory.CreateDirectory(folder);

        if (!File.Exists(Path.Combine(folder, fnViewer)))
            File.Copy(Path.Combine(renderers, fnViewer), Path.Combine(folder, fnViewer));

        if (!File.Exists(Path.Combine(folder, fnSettings)))
            File.Copy(Path.Combine(renderers, fnSettings), Path.Combine(folder, fnSettings));

        using (var file = File.CreateText(Path.Combine(folder, fnLink)))
        {
            var config = XsltDbUtils.GetConfig(ModuleId, TabId);

            XmlWriter xw = XmlWriter.Create(file);
            xw.WriteStartElement("XsltDbLink");
            xw.WriteStartElement("Config");
            xw.WriteAttributeString("id", config.ConfigID);
            xw.WriteAttributeString("name", config.Name);
            xw.WriteAttributeString("alias", config.ServiceName);
            xw.WriteAttributeString("is-super", config.IsSuper.ToString());
            xw.WriteAttributeString("xslt", config.ActiveXslt);

            xw.Close();
        }

        Message.Visible = true;

    }

    private void Return()
    {
        Response.Redirect(HttpUtility.UrlDecode(Request.Params["returnurl"]));
    }
}